import PermissionBtn from "@/components/PermissionBtn";
import { Button, Space, Table, TableProps, Pagination, Modal } from "antd";
import { ExclamationCircleFilled, PlusOutlined } from "@ant-design/icons";
import { useTimeLine } from "../hooks/useTimeLine";
import { type TimeLine } from "@/types/content/timeLine";
import TimeLineSearchForm from "./TimeLineSearchForm";
import { useTimeLineOption } from "../hooks/useTimeLineOption";
import TimeLineOption from "./TimeLineOption";

export default function TimeLine() {
  const {loading,tableData,pagination,setPagination,removeTimeLineInf,setSearchForm,getTableList}= useTimeLine()
  const {isModalOpen,openOptionDialog,oldInfo,optionType,close} = useTimeLineOption()
  const showConfirm = (row: TimeLine.Row) => {
    Modal.confirm({
      title: "警告",
      icon: <ExclamationCircleFilled />,
      content: `确认删除?`,
      okText: "确认",
      cancelText: "取消",
      okType: "danger",
      onOk() {
        removeTimeLineInf(row);
      },
    });
  };
  const columns: TableProps<TimeLine.Row>["columns"] = [
    {
      title: "时间",
      key: "time",
      dataIndex: "time",
      ellipsis: true,
      width: 100,
      align: "center",
    },
    {
      title: "内容",
      key: "content",
      dataIndex: "content",
      ellipsis: true,
      width: 180,
      align: "center",
    },

    {
      title: "操作",
      key: "action",
      fixed: "right",
      width: 100,
      align: "center",
      render: (_, row) => (
        <Space size={2} wrap>
          <PermissionBtn code="love.updateTimeLine">
            <Button
              type="link"
              onClick={() => {
                openOptionDialog("edit", row);
              }}
            >
              编辑
            </Button>
          </PermissionBtn>
          <PermissionBtn code="love.removeTimeLine">
            <Button
              type="link"
              danger
              onClick={() => {
                showConfirm(row);
              }}
            >
              删除
            </Button>
          </PermissionBtn>
        </Space>
      ),
    },
  ];

  return (
    <div className="page__wrap--g" style={{padding:0}}>
          <div className="form__wrap--g">
        <TimeLineSearchForm
          onChange={(form: TimeLine.Query) => {
            setSearchForm(form);
          }}
        />
        <PermissionBtn code="love.addTimeLine">
          <Button
            type="primary"
            icon={<PlusOutlined />}
            size="middle"
            onClick={() => {
              openOptionDialog('add')
            }}
          >
            新增时间轴
          </Button>
        </PermissionBtn>
      </div>
      <div className="table__wrap--g">
        <Table
          loading={loading}
          columns={columns}
          dataSource={tableData}
          rowKey="id"
          sticky
          bordered
          size="small"
          scroll={{ x: 1300 }}
          pagination={false}
        />
      </div>
      <div className="pagination__wrap--g">
        <Pagination
          pageSize={pagination.pageSize}
          current={pagination.pageNo}
          total={pagination.total}
          showQuickJumper={true}
          showTotal={(total) => `共${total}条`}
          onChange={(page, size) => {
            setPagination({
              total: pagination.total,
              pageNo: page,
              pageSize: size,
            });
          }}
        />
      </div>
      
      {isModalOpen && (
        <TimeLineOption
          isModalOpen={isModalOpen}
          optionType={optionType}
          oldInfo={oldInfo}
          close={close}
          change={() => {
            getTableList()
          }}
        />
      )}
    </div>
  );
}
